package com.xhx.stream.service;

import java.util.Random;

import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;

import cn.hutool.core.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class BusiService {

	@Retryable(value = { Exception.class }, maxAttempts = 4, backoff = @Backoff(delay = 2000))
	public void myMethod(String target) {
		log.info("尝试执行业务逻辑...{}", target);
		int i = RandomUtil.randomInt(1, 5);
		if (i < 3) {
			throw new RuntimeException("模拟异常");
		}
		log.info("成功执行...{}", target);
	}

	@Recover
	public void recover(Exception e) {
		log.error("重试失败，执行兜底逻辑,{}", e.getMessage());
	}

}
